package com.example.demo.controller;

import com.example.demo.domain.Result;
import com.example.demo.domain.User;
import com.example.demo.domain.dto.EditUserDTO;
import com.example.demo.domain.dto.RegisterDTO;


import com.example.demo.service.UserService;
import com.example.demo.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/auth")
@RestController
public class AuthController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody RegisterDTO registerDTO) {
        System.out.println(registerDTO);
        boolean check = userService.register(registerDTO);
        return check ? Result.ok() : Result.error();
    }

    @PostMapping("/login")
    public Result login(@RequestBody RegisterDTO login) {;
        Integer id = userService.login(login.getUsername(), login.getPassword());
        String token = JwtUtil.genToken(id);
        return id != null ? Result.ok(token) : Result.error();
    }
}
